package com.hello.pet.service.system.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hello.pet.common.result.ComResult;
import com.hello.pet.config.security.utils.SecurityUtils;
import com.hello.pet.service.system.dto.UserDto;
import com.hello.pet.service.system.model.User;
import com.hello.pet.service.system.param.UserParam;
import com.hello.pet.service.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author hello
 * @since 2022-04-01
 */
@RestController
@RequestMapping("/system/user")
@Api(tags="用户")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUserById")
    @ApiOperation("根据用户Id查询用户信息")
    public ComResult<UserDto> getUserById(@RequestParam @ApiParam("用户id") String id){
        return ComResult.success(BeanUtil.toBean(userService.getById(id), UserDto.class));
    }

    @GetMapping("/getUser")
    @ApiOperation("用户信息查询")
    public ComResult<UserDto> getUser(){
        String username = SecurityUtils.getUsername();
        return ComResult.success(BeanUtil.toBean(userService.findByUsername(username), UserDto.class));
    }

    @GetMapping("/deleteUser")
    @ApiOperation("删除用户信息")
    public ComResult deleteUser(@RequestParam @ApiParam("用户id") String id){
        return ComResult.success(userService.removeById(id));
    }

    @PostMapping("/addUser")
    @ApiOperation("新增用户")
    public ComResult addUser(@RequestBody UserParam user){
        userService.addUser(user);
        return ComResult.success();
    }

    @PostMapping("/selectUserList")
    @ApiOperation("查询用户列表")
    public ComResult<IPage<User>> selectUserList(@RequestBody Page<User> userPage){
        return ComResult.success(userService.page(userPage));
    }

    @GetMapping("/findPermissions")
    @ApiOperation("查找用户的接口权限标识集合")
    public ComResult<Set<String>> findPermissions(@RequestParam @ApiParam("用户名称") String username){
        return ComResult.success(userService.findPermissions(username));
    }

}
